package 力扣_树算法.搜索树;

/**701. 二叉搜索树中的插入操作
 * @author zx
 * @create 2022-04-16 12:14
 */
public class Num701 {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        //原树为空,新树根即添加的节点,否则,根节点不变
        if(root == null){
            return new TreeNode(val);
        }
        if(root.val > val){
            root.left = insertIntoBST(root.left,val);
        }else if(root.val < val){
            root.right = insertIntoBST(root.right,val);
        }
        return root;
    }
}
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}